package com.zkq.graduate.util;


import com.zkq.graduate.enums.ResponseEnum;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * @Author zhangkaiqiang
 * @Date 2019/3/6  16:45
 * @Description TODO
 */
@Component
@Data
public class ResultHandler<T>{

    private int code;

    private String msg;

    private T data;

    private int count;

    public ResultHandler(int code, String msg, T data, int count) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.count = count;
    }

    public ResultHandler() {
        this.code = 0;
        this.msg = "success";
    }

    public ResultHandler(T data){
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    public static ResultHandler success(){
        return new ResultHandler();
    }

    public static ResultHandler success(Object data){
        return new ResultHandler<>(data);
    }

    public static ResultHandler fail(ResponseEnum responseEnum){
        ResultHandler responseJson = new ResultHandler();
        responseJson.setCode(responseEnum.getCode());
        responseJson.setMsg(responseEnum.getMsg());
        return responseJson;
    }
}